<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller {

	//400 错误请求页面
	public function BadRequest(){
		$this->display('404');
	}

	/**
	 * [logout 退出登录]
	 * @return [type] [description]
	 */
    public function logout(){

        $this->redirect('/admin.php/Common/login');
    }


    /**
	 * [login 登录页面]
	 * @return [type] [description]
	 */
	public function login(){
        session('adminid',null);
		$this->display();
	}

	/**
	 * [verify 图片验证码]
	 * @return [type] [description]
	 */
	public function verify()
   	{
		$Verify = new \Think\Verify();
		$Verify->codeSet = "1234567890";
		$Verify->fontSize = 30;
		$Verify->length   = 4;
		$Verify->useNoise = true;
		$Verify->useCurve = false;
		$Verify->entry();
   	}


   	/**
	 * [ctrl_login 登录执行函数]
	 * @return [type] [description]
	 */
	public function ctrl_login(){
		if(IS_AJAX){
			$account = trim($_REQUEST['account']);
			$password = trim($_REQUEST['password']);
			$code = trim($_REQUEST['code']);

			$m_admin = M('Admin');

			//验证图片码
			$verify = new \Think\Verify();
			if($verify->check($code)===true){
				//查询账号是否存在
				$where['account'] = $account;
				$adminInfo = $m_admin->where($where)->find();
				if ($adminInfo['id']) {
					if (md5($password) == $adminInfo['password']) {
						session('adminid',$adminInfo['id']);
						session('adminovertime',(time()+60*60*C('adminovertime')));

						$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '登录成功'
		                ));
					}else{
						$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '密码错误'
		                ));
					}
				}else{
					$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '管理员不存在'
	                ));
				}
			}else{
				$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '验证码错误！'
                ));
			}
		}else{
			$this->redirect('/admin.php/Common/BadRequest');
		}
	}
}